/**
 * @author 张新林1
 * 时间 2016年11月15日下午8:51:35
 * 包名 com.xinhuo.employ.controller
 * 类名 UserController
 *
 */
package com.xinhuo.controller;

import com.xinhuo.domain.po.User;
import com.xinhuo.service.IUserService;
import com.xinhuo.util.EmojiFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * author 张新林
 * 时间 2019/4/3 23:15
 * 描述
 */
@Controller
@RequestMapping("/user")
public class UserController {

	Logger logger  =  LoggerFactory.getLogger(UserController.class);


	@Resource
	private IUserService userService;

	/**
	 * 跳转到首页
	 * http://localhost:8082/countgame/user/toGame
	 * @return
	 */
	@RequestMapping("toGame")
	public String toGame(String username,HttpServletRequest request){
		request.setAttribute("username",username);
		logger.info("into game");
		return "countGame";
	}

	/**
	 * 跳转到显示列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping("toGameList")
	public String toGameList(Model model){
		model.addAttribute("gameList",  userService.getUserByPage( 1, 5).getRecordList());
		return "gameList";
	}

	/**
	 * ajax分页
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping("loadMore")
	public List<User> loadMore(int page){
		//List<User> gameList = userService.getUserByPage("", page, 5).getRecordList();
		return userService.getUserByPage( page, 5).getRecordList();
	}

	/**
	 * 游戏开始
	 * @return
	 */
	@RequestMapping("startGame")
	public String startGame(String username,HttpSession session){
		session.setAttribute("startTime",new Date());
		session.setAttribute("username",username);
		logger.info("start game");
		return "countGame";
	}

	/**
	 * 更新用户游戏数据
	 * @param newUser
	 * @return
	 */
	@ResponseBody
	@RequestMapping("updateUser")
	public boolean updateUser(User newUser,HttpSession session){

		if(newUser.getLevel() == -1){
			//简单模式不计入排名也不保存
			return true;
		}
		Date startTime = (Date) session.getAttribute("startTime");
		if(!filterOperation(startTime)){
			return true;//伪装保存成功
		}
		//先判断是否已有数据，若有，则更新
		User preUser = userService.findUserByName(newUser.getUsername());
		if(preUser!=null&&preUser.getCorrectSum()<newUser.getCorrectSum()){
			//更新
			preUser.setSex(newUser.getSex());
			preUser.setCountSum(newUser.getCountSum());
			preUser.setCorrectSum(newUser.getCorrectSum());
			preUser.setLevel(newUser.getLevel());
			userService.updateUser(preUser);
			logger.info("game update user:{},correctSum:{}",preUser.getUsername(),preUser.getCorrectSum());
		}else if(preUser==null){
			//插入
			newUser.setUsername(EmojiFilter.EmojiFilter(newUser.getUsername()));
			userService.save(newUser);
			logger.info("game save user:{},correctSum:{}",newUser.getUsername(),newUser.getCorrectSum());
		}
		return true;
	}

	private boolean filterOperation(Date startTime){
		if(startTime == null){
			logger.info("game save user error, startTime is null}");
			return false;
		}
		long time =( System.currentTimeMillis() - startTime.getTime())/1000;
		if( time<95 || time > 125){
			logger.info("game save user error, 恶意操作}");
			return false;
		}
		return true;
	}
}
